﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ParieurPro.DAL;
using ParieurPro.DAL.DatabaseModel;

namespace MyBet.Web.Controls
{
    public partial class Rank : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["CurrentUser"] != null)
                {
                    using (MyBetDBEntities dbContext = new MyBetDBEntities())
                    {
                        UserObject user = Session["CurrentUser"] as UserObject;
                        List<EventObject> events = RankObject.GetEventsWithRankFromCustomContestId(dbContext, user.CustomContestId);
                        EventList.Items.Add(new ListItem("Général", "0"));
                        foreach (EventObject item in events)
                        {
                            EventList.Items.Add(new ListItem(String.Format("Journée {0}", item.DayNumber), item.EventId.ToString()));
                        }
                        BindData(dbContext, user.CurrentContestId, System.Convert.ToInt32(EventList.SelectedValue)); 
                    }
                }
            }
        }

        public void BindData(MyBetDBEntities dbContext, int contestId, int eventId)
        {

            List<RankObject> ranks = RankObject.GetCustomContestRanksFromEvent(dbContext, contestId, eventId);

            DataSet rankDS = new DataSet();
            DataTable rankTable = rankDS.Tables.Add();

            //-- Add columns to the data table
            rankTable.Columns.Add("Rank", typeof(int));
            rankTable.Columns.Add("Login", typeof(string));
            rankTable.Columns.Add("TotalScore", typeof(long));
            rankTable.Columns.Add("RankDifferential", typeof(int));

            //-- Add rows to the data table
            foreach (RankObject item in ranks)
            {
                String userName = item.UserName;

                if (((item.BestOfEventCount.HasValue) && (item.BestOfEventCount.Value != 0))
                    || ((item.BestOfEventInProgressCount.HasValue) && (item.BestOfEventInProgressCount.Value != 0)))
                {
                    userName += " ";
                }

                if ((item.BestOfEventCount.HasValue) && (item.BestOfEventCount.Value!=0))
                { 
                    int i = 0;
                    
                    while (i<item.BestOfEventCount)
                    {
                        userName += "<b>*</b>";
                        i++;
                    }
                }
                if ((item.BestOfEventInProgressCount.HasValue) && (item.BestOfEventInProgressCount.Value != 0))
                {
                    int i = 0;
                    while (i < item.BestOfEventInProgressCount)
                    {
                        userName += "*";
                        i++;
                    }
                }
                rankTable.Rows.Add(item.Rank, userName, item.TotalScore, item.RankDifferential);
            }

            RankGrid.DataSource = rankDS;
            RankGrid.DataBind();
        }

        protected void EventList_SelectedIndexChanged(object sender, EventArgs e)
        {
            UserObject user = Session["CurrentUser"] as UserObject;
            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {
                BindData(dbContext, user.CurrentContestId, System.Convert.ToInt32(EventList.SelectedValue));
            }
        }

        protected void RankGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            UserObject user = Session["CurrentUser"] as UserObject;
            RankGrid.CurrentPageIndex = e.NewPageIndex;
            // Rebind the data to refresh the DataGrid control. 
            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {
                BindData(dbContext, user.CurrentContestId, System.Convert.ToInt32(EventList.SelectedValue));
            }
        }

        protected void RankGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
        {

            UserObject user = Session["CurrentUser"] as UserObject;

            // Use the ItemDataBound event to customize the DataGrid control. 
            // The ItemDataBound event allows you to access the data before 
            // the item is displayed in the control.
            if (((e.Item.ItemType == ListItemType.Item) ||
              (e.Item.ItemType == ListItemType.AlternatingItem)))
            {
                int differential = 0;
                String userNameToTest = e.Item.Cells[1].Text;
                userNameToTest = userNameToTest.Replace("<b>","");
                userNameToTest = userNameToTest.Replace("</b>","");
                userNameToTest = userNameToTest.Replace("*","").Trim();
                if (String.Compare(user.UserName, userNameToTest , true) == 0)
                {
                    e.Item.BackColor = System.Drawing.Color.BlanchedAlmond;
                    e.Item.Font.Bold = true;
                }
                e.Item.Cells[3].HorizontalAlign = HorizontalAlign.Center;
                if (Int32.TryParse(e.Item.Cells[3].Text, out differential))
                {
                    if (differential == 0)
                    {
                        e.Item.Cells[3].ForeColor = System.Drawing.Color.Black;
                        e.Item.Cells[3].Text = "-";
                    }
                    else if (differential < 0)
                    {
                        e.Item.Cells[3].ForeColor = System.Drawing.Color.Red;
                    }
                    else if (differential > 0)
                    {
                        e.Item.Cells[3].ForeColor = System.Drawing.Color.Green;
                        if (!e.Item.Cells[3].Text.StartsWith("+"))
                        {
                            e.Item.Cells[3].Text = "+" + e.Item.Cells[3].Text;
                        }
                    }
                }
                else
                {
                    e.Item.Cells[3].ForeColor = System.Drawing.Color.Black;
                    e.Item.Cells[3].Text = "-";
                }

            }

        }


    }
}